home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0034_Word Wrapping.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1994-01-27  |  2.8 KB  |  91 lines

  1. {
  2. > ..Well.. I am back at writing a chat door for the third time.. and am
  3. > havin trouble with wrapping the text.  It seems that when it wraps the
  4. > text to the next line it won't remove the text on the previous line,
  5. > and sometimes it won't wrap at all..  I don't have very dependable code
  6. > for this purpose so any help code is appreciated.. (I am using RMdoor
  7. > 4.2 right now..anybody seen anything better??).
  8.  
  9. Hope this helps...
  10. }
  11. {$R-,S-,I+,D+,F-,V+,B-,N-,L+ }
  12. {$M 2048,0,0 }
  13.  
  14. PROGRAM WordWrap(INPUT,OUTPUT);
  15. USES CRT;
  16.  
  17. CONST
  18.    FKeyCode          = #0;
  19.    Space             = ' ';
  20.    Hyphen            = '-';
  21.    BackSpace         = ^H;
  22.    CarriageReturn    = ^M;
  23.    MaxWordLineLength = 80;
  24.  
  25. VAR
  26.    WordLine  : STRING[MaxWordLineLength];
  27.    Index1    : BYTE;
  28.    Index2    : BYTE;
  29.    InputChar : CHAR;
  30.  
  31. BEGIN
  32.   WordLine  := '';
  33.   Index1    := 0;
  34.   Index2    := 0;
  35.   InputChar := Space;
  36.  
  37.   AssignCRT(INPUT);
  38.   AssignCRT(OUTPUT);
  39.   Reset(INPUT);
  40.   ReWrite(OUTPUT);
  41.   Writeln('Enter text (ENTER to stop) : ');
  42.  
  43.   InputChar := READKEY;
  44.  
  45.   {Do the job.}
  46.   WHILE (InputChar <> CarriageReturn) DO
  47.     BEGIN
  48.       CASE InputChar OF
  49.         BackSpace: {write destructive backspace & remove char from WordLine}
  50.           BEGIN
  51.             Write(OUTPUT,BackSpace,Space,BackSpace);
  52.             Delete(WordLine,(LENGTH(WordLine) - 1),1)
  53.           END;
  54.         FKeyCode: {user pressed a function key, so dismiss it}
  55.           BEGIN
  56.             InputChar := READKEY; {function keys send two-char scan code!}
  57.             InputChar := Space
  58.           END
  59.         ELSE {InputChar contains a valid char, so deal with it}
  60.           BEGIN
  61.             Write(OUTPUT,InputChar);
  62.             WordLine := (WordLine + InputChar);
  63.             IF (Length(WordLine) >= (MaxWordLineLength - 1)) THEN
  64.              {we have to do a word-wrap}
  65.               BEGIN
  66.                 Index1 := (MaxWordLineLength - 1);
  67.                 WHILE ((WordLine[Index1] <> Space)
  68.                   AND (WordLine[Index1] <> Hyphen) AND (Index1 <> 0))
  69.                     DO Index1 := (Index1 - 1);
  70.                       IF (Index1 = 0)
  71.                         THEN  {whoah, no space was found to split line!}
  72.                           Index1 := (MaxWordLineLength - 1); {forces split}
  73.                       Delete(WordLine,1,Index1);
  74.                       FOR Index2 := 1 TO LENGTH(WordLine) DO
  75.                         Write(OUTPUT,BackSpace,Space,BackSpace);
  76.                       Writeln(OUTPUT);
  77.                       Write(OUTPUT,WordLine)
  78.               END
  79.           END
  80.       END; {CASE InputChar}
  81.       {Get next key from user.}
  82.       InputChar := READKEY
  83.     END; {WHILE (InputChar <> CarriageReturn)}
  84.  
  85.   {Wrap up the program.}
  86.   Writeln(OUTPUT);
  87.   Writeln(OUTPUT);
  88.   Close(INPUT);
  89.   Close(OUTPUT)
  90. END.
  91.